<template>
	<view class="container">
		<uni-list v-if="showStatus">
			<uni-list-item showExtraIcon="true" :extraIcon="{type: 'phone-filled'}" title="猪只耳号"
				:rightText="pigBreedList.pigCode" />
			<uni-list-item showExtraIcon="true" :extraIcon="{type: 'location-filled'}" title="配种方式"
				:rightText="pigBreedType" />
			<uni-list-item showExtraIcon="true" :extraIcon="{type: 'person-filled'}" title="配种公猪编号"
				:rightText="pigBreedList.pigFatherCode" />
			<uni-list-item showExtraIcon="true" :extraIcon="{type: 'calendar-filled'}" title="配种时间"
				:rightText="pigBreedList.pigBreedTime" />
			<uni-list-item showExtraIcon="true" :extraIcon="{type: 'person-filled'}" title="配种管理员"
				:rightText="pigBreedList.hmyStaff.staffName" />
			<uni-list-item showExtraIcon="true" :extraIcon="{type: 'chat-filled'}" title="备注">
				<template v-slot:footer>
					<input type="textarea" v-model="pigBreedList.remark"
						style="font-size: 12px;color: #999999;text-align: right;" />
				</template>
			</uni-list-item>

		</uni-list>
		<view class="button-group" v-if="showStatus">
			<button type="warn" plain="true" @click="pigBreedDelete" style="width: 45%;border: #E53F32 solid 1px;">删 除</button>
			<button type="primary" plain="ture" @click="pigBreedEdit" style="width: 45%;">修 改</button>
		</view>

		<!-- 修改 -->
		<view class="example" v-if="!showStatus">
		  <uni-forms ref="form" :model="pigBreedForm"   :rules="rules" labelWidth="80px">
		    <uni-forms-item label="猪只耳号" name="pigCode" required>
		      <uni-easyinput v-model="pigBreedForm.pigCode" placeholder="请输入猪只耳号" />
		    </uni-forms-item>
			<uni-forms-item label="配种方式" name="pigBreedType" required>
			  <uni-data-select v-model="pigBreedForm.pigBreedType" :placeholder="pigBreedType"
			  	:localdata="pigBreedTypes"
			    ></uni-data-select>
			</uni-forms-item>
			<uni-forms-item label="配种公猪编号" name="pigFatherCode" required>
			  <uni-easyinput v-model="pigBreedForm.pigFatherCode" placeholder="请输入配种公猪耳号" />
			</uni-forms-item>
			<uni-forms-item label="配种时间" name="pigBreedTime" required>
			  <uni-datetime-picker type="datetime" v-model="pigBreedForm.pigBreedTime"/>
			</uni-forms-item>
			<uni-forms-item label="配种管理员" name="pigBreedManager" required>
			  <uni-data-select
			   v-model="pigBreedForm.pigBreedManager"
			  :placeholder="pigBreedForm.hmyStaff.staffName"
			  	:localdata="staffNameList"
			    ></uni-data-select>
			</uni-forms-item>
			<uni-forms-item label="备注">
					<uni-easyinput type="textarea" v-model="pigBreedForm.remark" placeholder="请输入备注" />
				</uni-forms-item>
			  </uni-forms>
			  <view class="edit-button-group">
			  	<button type="primary" @click="back" style="width: 45%;">返 回</button>
			  	<button type="primary" @click="editPidBreed('form')" style="width: 45%;">提 交</button>
			  </view>
			</view>
	</view>
</template>

<script>
	import {
		findAllStaffName,getPigBreed,delPigBreed,updatePigBreed
	} from "@/api/hmy/pigBreed";

	export default {
		data() {
			return {
				showStatus:true,
				pigBreedId: '',
				pigBreedList: {
					hmyStaff:{
						staffName:''
					}
				},
				pigBreedType: '',
				pigBreedForm:'',
				pigBreedTypes: [{
				  text: ' 人工授精',
				  value: "1"
				},
				{
				  text: ' 自然交配',
				  value: "2"
				}],
				rules: {
					pigCode: {
						rules: [{required: true, errorMessage: '猪只耳号不能为空'}]
					},
					pigBreedType: {
						rules: [{required: true, errorMessage: '配种方式不可为空'}]
					},
					pigFatherCode: {
				  		rules: [{required: true, errorMessage: '配种公猪编号不能为空'}]
					},
					pigBreedTime: {
						rules: [{required: true, errorMessage: '配种时间不能为空'}]
					}
				},
				//所有物料负责人
				staffNameList: [],
			}
		},

		created() {
			findAllStaffName().then(response => {
				var staffList = response.data;
				staffList.forEach(item => {
					this.staffNameList.push({
						'text': item.staffName,
						'value': item.staffId
					})
				})
				//console.log(this.staffNameList);
			})
		},


		onLoad(option) { //option为object类型，会序列化上个页面传递的参数
			//console.log(option.id);
			this.pigBreedId = option.id;
			this.getPigBreedInfo();
		},
		methods: {
			getPigBreedInfo() {
				getPigBreed(this.pigBreedId).then(response => {
					this.pigBreedForm = response.data;
					this.pigBreedList = response.data;
					// 两个字典类型
					if (this.pigBreedList.pigBreedType == 1) {
						this.pigBreedType = '人工授精'
					} else if (this.pigBreedList.pigBreedType == 2) {
						this.pigBreedType = '自然交配'
					}

					//隐藏加载框
					uni.hideLoading();
					// console.log(response)

				})
			},

			//删除
			pigBreedDelete() {
				console.log("delete", this.pigBreedList.pigBreedId)
				this.$modal.confirm('是否确认删除进货编号为"' +this.pigBreedList.pigBreedId + '"的数据项？').then(() => {
					return delPigBreed(this.pigBreedList.pigBreedId);
				}).then(response => {
					this.$modal.msgSuccess("删除成功");
					this.$tab.navigateTo('/pages/hmy/pigBreed/index');
				}).catch(()=>{

				});
			},

			//修改
			pigBreedEdit() {
				console.log("edit", this.pigBreedList.pigBreedId)
				this.showStatus = !this.showStatus
			},
			//返回
			back(){
				this.showStatus = !this.showStatus
				this.getPigBreed();
			},

			//修改提交
			editPidBreed(form) {
			  this.$refs[form].validate().then(res => {
			    updatePigBreed(this.pigBreedForm).then(response => {
			      this.$modal.msgSuccess("修改成功");
				  setTimeout(()=>{
				  	this.$tab.navigateTo('/pages/hmy/pigBreed/index');
				  },1000)

			    })
			  })
			}
		}
	}
</script>

<style>
	page {
		background-color: #ffffff;
	}

	.button-group {
		display: flex;
		justify-content: space-around;
		width:100%;
		position:fixed;
		bottom:5px;

	}

	.example {
	  padding: 15px;
	  background-color: #fff;
	}
	.edit-button-group{
		display: flex;
		justify-content: space-around;
	}
</style>
